package com.jiangjiang.eshop.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.auth.dao.AccountMapper;
import com.jiangjiang.eshop.auth.domain.AccountEntity;
import com.jiangjiang.eshop.auth.dto.AccountDTO;
import com.jiangjiang.eshop.auth.dto.AccountQueryPageDTO;

import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

/**
 * @ClassName AccountService
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/7 5:15
 * @Version 1.0
 **/
@Service
public class AccountService extends ServiceImpl<AccountMapper, AccountEntity> {
    /**
     * 用户账户分页查询
     *
     * @param dto 查询条件
     * @return 查询结果
     */
    public Page<AccountEntity> queryForPage(AccountQueryPageDTO dto) {
        Page<AccountEntity> page = new Page<>(dto.getPageNo(), dto.getPageSize(), true);
        LambdaQueryWrapper<AccountEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Strings.isEmpty(dto.getUsername()), AccountEntity::getUsername, dto.getUsername())
                .eq(Strings.isEmpty(dto.getName()), AccountEntity::getName, dto.getName());
        return this.getBaseMapper().selectPage(page, wrapper);
    }

    /**
     * 根据id查询用户账户数据
     * @param accountId 用户账户id
     * @return 查询结果
     */
    public AccountEntity queryAccountById(Long accountId) {
        LambdaQueryWrapper<AccountEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(AccountEntity::getId, accountId);
        return this.getOne(wrapper);
    }

    /**
     * 更新用户账户密码
     * @param accountId
     * @param password
     * @return 处理结果
     */
    public Boolean updatePassword(Long accountId, String password) {
        LambdaUpdateWrapper<AccountEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(AccountEntity::getId, accountId)
                .set(AccountEntity::getPassword, password);
        return this.update(wrapper);
    }
}
